package com.baor.controller;

import com.baor.config.TokenService;
import com.baor.service.ExceptionReportService;
import com.baor.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/exceptionReport")
@Slf4j
public class ExceptionReportController {
    @Autowired
    private ExceptionReportService exceptionReportService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取设备异常上报列表
     * @return
     */
    @RequestMapping("/getList")
    public R getList(@RequestBody Map<String, Object> params) {
        log.info("设备异常上报列表..... params: {}", params);
        R r = this.exceptionReportService.getList(params);
        return R.ok(r);
    }
    @RequestMapping("/getInfo/{id}")
    public R getInfo(@PathVariable("id") Integer id) {
        log.info("设备异常上报详情列表..... params: {},id:{}",id);
        R r = this.exceptionReportService.getInfo(id);
        return R.ok(r);
    }

    /**
     * TODO 删除设备异常上报
     */
    @RequestMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id) {
        log.info("设备异常上报列表..... params: {},id:{}",id);
        R r = this.exceptionReportService.del(id);
        return R.ok(r);
    }
    /**
     * TODO 修改设备异常上报
     */
    @RequestMapping("/update")
    public R update(@RequestBody Map<String, Object> params,@RequestHeader(value = "token") String token) {
        log.info("修改列表..... params: {}", params);
        if (token == null) {
            return R.er("未登录或token不存在");
        }
        Integer reviewerUserId = tokenService.getUserId(token);
        if (reviewerUserId == null) {
            return R.er("token无效");
        }
        params.put("reviewerUserId", reviewerUserId);
        R r = this.exceptionReportService.update(params);
        return R.ok(r);
    }

}
